import { useAuthStore } from "~/store/auth";
import type { UserRole } from "~/types";
export default defineNuxtRouteMiddleware((to) => {
  const authStore = useAuthStore();

  // 如果是 /error 路由，直接返回
  if (to.path === "/error") return;

  // 权限的配置
  const permissions: Record<UserRole, string[]> = {
    admin: ["/", "/dashboard", "/admin", "/editor"],
    editor: ["/", "/dashboard", "/editor"],
    guest: ["/", "/dashboard"],
  };

  // 获取当前用户的角色
  const userRole = authStore.userRole;

  // 检查访问权限
  if (
    !permissions[userRole as "admin" | "editor" | "guest"].includes(to.path)
  ) {
    return navigateTo("/error?message=forbidden");
  }
});
